package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  @PathVariable("type") Integer type,
                                  String nickname) {
        PageResult pageResult = userInfoService.findByType(page,pagesize,type,nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 添加喜欢
     * @param uid
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity addLike(@PathVariable("uid") Long uid){
        userInfoService.addLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消喜欢
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleLike(@PathVariable("uid") Long uid){
        userInfoService.deleLike(uid);
        return ResponseEntity.ok(null);
    }
}
